package org.example.code.test2024.ten_mouth;

import java.util.Scanner;

public class test1015 {

    private void test01(){
        Scanner in = new Scanner(System.in);
        int t = in.nextInt();
        while(t>0){
            long n = in.nextLong();
            long k = in.nextLong();
            long sum = (n+1)/2;
            if(k<=sum){
                System.out.println(sum);
            }else{
                //奇数
                if(n%2==1){
                    System.out.println(sum-(k-sum));
                }else{
                    System.out.println(sum-(k-sum-1));
                }
            }
            t--;
        }
    }

    private void test02(String str){
        StringBuilder sb = new StringBuilder(str);
        for(int i=0;i<str.length();i++){
            char c = sb.charAt(i);
            sb.deleteCharAt(i);
            sb.append(c);
        }
        System.out.println(sb);
    }
}
